package com.maidada.mddaichat.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.maidada.mddaichat.common.BaseRequest;
import com.maidada.mddaichat.common.BaseResponse;
import com.maidada.mddaichat.common.ResultUtils;
import com.maidada.mddaichat.dto.user.*;
import com.maidada.mddaichat.service.UserService;
import com.maidada.mddaichat.vo.user.UserDetailVO;
import com.maidada.mddaichat.vo.user.UserPageVO;
import com.maidada.mddaichat.vo.user.UserVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * [用户表]控制层
 *
 * @author wulinxuan
 * @date 2025-04-16 16:15
 */
@Slf4j
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    @PostMapping("/register")
    public BaseResponse<String> registerUser(@Valid @RequestBody UserRegisterRequest param) {
        String token = userService.register(param);
        return ResultUtils.success(token);
    }

    @PostMapping("/login")
    public BaseResponse<String> loginUser(@Valid @RequestBody UserLoginRequest param) {
        String token = userService.login(param);
        return ResultUtils.success(token);
    }

    @PostMapping("/currentUser")
    public BaseResponse<UserVO> currentUser() {
        UserVO result = userService.currentUser();
        return ResultUtils.success(result);
    }

    @PostMapping("/add")
    public BaseResponse<String> addUser(@Valid @RequestBody UserAddRequest param) {
        userService.add(param);
        return ResultUtils.successWithMsg("新增成功");
    }

    @PostMapping("/delete")
    public BaseResponse<String> deleteUser(@Valid @RequestBody BaseRequest param) {
        userService.delete(param);
        return ResultUtils.successWithMsg("删除成功");
    }

    @PostMapping("/update")
    public BaseResponse<String> updateUser(@Valid @RequestBody UserUpdateRequest param) {
        userService.update(param);
        return ResultUtils.successWithMsg("更新成功");
    }

    @GetMapping("/detail")
    public BaseResponse<UserDetailVO> queryUserDetail(@Valid BaseRequest param) {
        UserDetailVO result = userService.queryDetail(param);
        return ResultUtils.success(result);
    }

    @GetMapping("/page")
    public BaseResponse<IPage<UserPageVO>> queryUserPage(UserPageRequest param) {
        IPage<UserPageVO> result = userService.queryPage(param);
        return ResultUtils.success(result);
    }
}
